<?php

namespace IDock\Insurance\QHIC\Request;

use IDock\Insurance\QHIC\QHICEnums;
use IDock\Insurance\QHIC\Request\AllQHICRequest;
use IDock\Insurance\QHIC\Response\VehicleActualValueResponse;
use IDock\Visitor\VehicleActualValueVisitor;

class VehicleActualValueRequest extends AllQHICRequest
{
    public $requestType = 'VHL0011';

    protected $response = VehicleActualValueResponse::class;

    protected $urlTip = '车辆实际价值';

    /*** @var  VehicleActualValueVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        $arr = [
            'CFstRegYm' => date('YmdHis',strtotime($this->visitor->firstRegisterDate)),
            'TInsrncBgnTm' => date('YmdHis',strtotime($this->visitor->startDate)),
            'CModelCde' => $this->visitor->vehicleModelCode,
            'CUsageCde' => QHICEnums::$causeType[$this->visitor->causeType] ?? '',
            'NSeatNum' => $this->visitor->vehicleSeat,
            'CVhlTyp' => $this->getCVhlTyp(),
        ];
        if(empty($arr['CVhlTyp'])){
            throw new \Exception("不支持该车辆类型");
        }
        if(empty($arr['CUsageCde'])){
            throw new \Exception("不支持该车辆用途");
        }

        return $arr;
    }

    private function getCVhlTyp()
    {
        $code = '';
        $seat = $this->visitor->vehicleSeat;
        $carKind = $this->visitor->carKindCode;
        $weight = round(intval($this->visitor->argc['wholeWeight'] ?? '') / 1000,2);
        if($carKind == '100'){
            //客车
            if($seat < '6'){
                $code = '365001';
            }elseif($seat < '10'){
                $code = '365002';
            }elseif($seat < '20'){
                $code = '365003';
            }elseif($seat < '36'){
                $code = '365004';
            }else{
                $code = '365005';
            }
        }elseif ($carKind == '200'){
            //货车
            if($weight < '2'){
                $code = '365006';
            }elseif ($weight < '5'){
                $code = '365007';
            }elseif ($weight < '10'){
                $code = '365008';
            }else{
                $code = '365009';
            }
        }
        if(empty($code)){
            throw new \Exception("不支持的车辆种类");
        }
        return $code;
    }

}
